package com.gqr.uncaughtException;

/**
 * Copyright (C), 2020-2024,gqr
 * FileName:CantCatchDirectly
 * author:guqingrong
 * Date:2024-04-25 21:42
 * Description:
 * 1.不加try/catch 抛出4个异常,都带有线程名字
 * 2. 加了try/catch,期望捕获到第一个线程的异常,线程234不应该运行,希望看到打印出caughtException
 * 3. 执行时发现,根本就不由caughtException ,线程234依然运行并且抛出异常
 * 说明线程的异常不能用传统方法捕获
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class CantCatchDirectly implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        try {
            new Thread(new CantCatchDirectly(), "线程1").start();
            Thread.sleep(300);

            new Thread(new CantCatchDirectly(), "线程2").start();
            Thread.sleep(300);

            new Thread(new CantCatchDirectly(), "线程3").start();
            Thread.sleep(300);

            new Thread(new CantCatchDirectly(), "线程4").start();
        } catch (RuntimeException e) {
            System.out.println("捕获到异常");
        }


    }

    @Override
    public void run() {
        try {
            throw new RuntimeException();
        }catch (RuntimeException e){
            System.out.println("在run方法内捕获到异常");
        }

    }
}
